#!/bin/bash -e
#

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
obmc_dir=${script_dir}/../../

cd "$obmc_dir"

# openbmc doesn't control what upstream poky, or any of the other layers do,
# which do use patches as part of their upstreaming process.
# meta-phosphor is also included such that patches that the community agrees to
# hold onto will be allowed in that layer.

patch_files_tmp=$(mktemp)
allowed_patches_tmp=$(mktemp)
trap 'rm $patch_files_tmp $allowed_patches_tmp' exit

git ls-files -- \
  '*.patch' '*.dts' \
  ':!:poky/**' \
  ':!:meta-arm/**' \
  ':!:meta-security/**' \
  ':!:meta-raspberrypi/**' \
  ':!:meta-openembedded/**' \
  ':!:meta-phosphor/**' \
  | sort > "$patch_files_tmp"


# The following patches were present on master at the time this test was
# written.  Their presence in this list should not be acknowlegement that they
# are now allowed, but ignoring them is required in the intermediate time
# between when this test was created, and when the maintainers of these repos
# clean them up.
#
# https://github.com/openbmc/docs/blob/master/meta-layer-guidelines.md
echo "\
meta-aspeed/recipes-aspeed/python/socsec/0001-otptool-Define-value_start-in-rev_id-path.patch
meta-aspeed/recipes-bsp/u-boot/files/default-gcc.patch
meta-facebook/meta-yosemitev2/recipes-bsp/u-boot/u-boot-aspeed-sdk/0001-board-aspeed-Add-Mux-for-yosemitev2.patch
meta-facebook/meta-yosemitev2/recipes-bsp/u-boot/u-boot-aspeed-sdk/0002-spl-host-console-handle.patch
meta-google/dynamic-layers/nuvoton-layer/recipes-bsp/images/npcm7xx-igps/0001-Set-FIU0_DRD_CFG-and-FIU_Clk_divider-for-gbmc-hoth.patch
meta-google/recipes-extended/libconfig/files/0001-conf2struct-Use-the-right-perl.patch
meta-google/recipes-extended/libconfig/files/0001-makefile-Add-missing-LDFLAGS.patch
meta-google/recipes-phosphor/initrdscripts/obmc-phosphor-initfs/rwfs-clean-dev.patch
meta-hpe/meta-dl360poc/recipes-kernel/linux/linux-obmc/gxp.dts
meta-ingrasys/meta-zaius/recipes-bsp/u-boot/u-boot-aspeed/0001-board-aspeed-Add-reset_phy-for-Zaius.patch
meta-nuvoton/recipes-bsp/images/npcm7xx-igps/0001-Adjust-paths-for-use-with-Bitbake.patch
meta-yadro/meta-nicole/recipes-bsp/u-boot/files/0001-Add-system-reset-status-support.patch
meta-yadro/meta-nicole/recipes-bsp/u-boot/files/0002-config-ast-common-set-fieldmode-to-true.patch
meta-yadro/meta-nicole/recipes-bsp/u-boot/files/0003-aspeed-add-gpio-support.patch
meta-yadro/meta-nicole/recipes-bsp/u-boot/files/0004-aspeed-add-bmc-position-support.patch
meta-yadro/meta-nicole/recipes-kernel/linux/linux-aspeed/0001-Add-NCSI-channel-selector.patch
meta-yadro/meta-nicole/recipes-phosphor/host/op-proc-control/0001-Stop-and-send-SRESET-for-one-thread-only.patch
meta-yadro/recipes-phosphor/dbus/phosphor-dbus-interfaces/0001-Add-boot-initiator-mailbox-interface.patch
meta-yadro/recipes-phosphor/ipmi/phosphor-ipmi-host/0001-Add-support-for-persistent-only-settings.patch
meta-yadro/recipes-phosphor/ipmi/phosphor-ipmi-host/0002-Add-support-for-boot-initiator-mailbox.patch
meta-yadro/recipes-phosphor/ipmi/phosphor-ipmi-host/0003-Fix-version-parsing-update-AUX-revision-info.patch
" | sort > "$allowed_patches_tmp"

files_diff=$(comm -23 "$patch_files_tmp" "$allowed_patches_tmp")

files_count=$(echo -n "$files_diff" | grep -c '^' || true)
if [[ $files_count -ne 0 ]]; then
  echo "Patch or DTS files found not in allow list"
  echo "$files_diff"
  echo "Patches are not allowed on OpenBMC in these layers.  Please upstream your changes and see \
    https://github.com/openbmc/docs/blob/master/meta-layer-guidelines.md"
  exit 1
fi

# Now verify there are no kernel dts files being added in a patch
# All dts files should be sent upstream and can be carried in the openbmc/linux
# tree until they are accepted upstream
dts_files_tmp=$(mktemp)
allowed_dts_tmp=$(mktemp)
trap 'rm $dts_files_tmp $allowed_dts_tmp' exit

git ls-files -- \
  '*.dts' \
  ':!:poky/**' \
  ':!:meta-arm/**' \
  ':!:meta-security/**' \
  ':!:meta-raspberrypi/**' \
  ':!:meta-openembedded/**' \
  | sort > "$dts_files_tmp"

# There is a single dts currently in the tree that we will exempt for now
echo "\
meta-hpe/meta-dl360poc/recipes-kernel/linux/linux-obmc/gxp.dts
" | sort > "$allowed_dts_tmp"

files_diff=$(comm -23 "$dts_files_tmp" "$allowed_dts_tmp")

files_count=$(echo -n "$files_diff" | grep -c '^' || true)
if [[ $files_count -ne 0 ]]; then
echo "Dts files found not in allow list"
echo "$files_diff"
echo "Dts files are not allowed on OpenBMC in these layers. Please upstream your changes and see \
  https://github.com/openbmc/docs/blob/master/kernel-development.md"
exit 1
fi

lint_exempt="\
meta-phosphor/recipes-support/nss-pam-ldapd/files/nslcd.init
"

types=(json shell)
# shellcheck disable=SC2034
check_json="eslint --resolve-plugins-relative-to /usr/local/lib/node_modules"
# shellcheck disable=SC2034
check_shell="shellcheck -x"

for t in "${types[@]}"; do
    check_cmd="check_${t}"
    if ! which "${!check_cmd%% *}" > /dev/null 2>&1; then
        eval "${check_cmd}=\"echo WARNING: Skipping $t due to missing command:\""
        echo "${!check_cmd}"
    fi
done

non_bbfiles=$(git ls-files -- \
  ':!:poky/**' \
  ':!:meta-arm/**' \
  ':!:meta-security/**' \
  ':!:meta-raspberrypi/**' \
  ':!:meta-openembedded/**' \
  | grep -v -e "\.patch$" -e "\.bb$" -e "\.bbappend$")

for f in $non_bbfiles; do
    unset file_type
    file_info=$(file "$f")
    case $file_info in
        *shell\ script*)
            file_type="shell"
            ;;

        *JSON\ data*)
            file_type="json"
            ;;

        *)
            case $f in
                *.sh)
                    file_type="shell"
                    ;;

                *.json)
                    file_type="json"
                    ;;
            esac
    esac

    if [ -n "$file_type" ]; then
        check_cmd="check_${file_type}"
        if ! eval "${!check_cmd} $f"; then
            if [[ $lint_exempt == *$f* ]]; then
                echo "EXEMPT: $f"
            else
                echo "FAILED: $f"
                false
            fi
        fi
    fi

done

# Regenerate supported machines list.
meta-phosphor/scripts/generate-machine-list
if ! git --no-pager diff --exit-code ; then
    echo "Changes to supported-machines.md necessary due to new meta-layer support."
    exit 1
fi

echo "Repo test passed"
